package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.DishFlavorDao;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired(required = false)
    private DishDao dishDao;
    @Autowired(required = false)
    private DishFlavorDao dishFlavorDao;
    @Autowired(required = false)
    private CategoryDao categoryDao;
    @Override
    public void save(DishDto dishDto) {
        dishDto.setStatus(1);
        dishDto.setSort(0);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        // 添加菜品
        dishDao.save(dishDto);
        // 添加口味
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        for (DishFlavor dishFlavor : dishFlavorList) {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setIsDeleted(0);
        }

        dishFlavorDao.bactSave(dishFlavorList);
    }

    @Override
    public Page<DishDto> getByPage(Integer page, Integer pageSize, String name) {
        // 设置当前页及页面大小
        PageHelper.startPage(page, pageSize);
        // 获取基本数据
        List<Dish> dishDaoList = dishDao.getByPage(name);
        // 创建pangeInfo
        PageInfo<Dish> pageInfo = new PageInfo<>(dishDaoList);

        // stream流拼装数据
        List<DishDto> dishDtoList = dishDaoList.stream().map(dishDao -> {
            DishDto dishDto = new DishDto();
            // 对象拷贝
            BeanUtils.copyProperties(dishDao, dishDto);
            // 获取分类数据
            Category category = categoryDao.findByCateId(dishDao.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());

        // 分装page对象
        Page<DishDto> dishDtoPage = new Page<>(dishDtoList, pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());
        return dishDtoPage;
    }

    @Override
    public DishDto getById(Long id) {
        // 根据id获取菜品信息
        Dish dish = dishDao.getById(id);
        // 根据id获取口味信息
        List<DishFlavor> dishFlavor = dishFlavorDao.getByDishId(id);
        // 封装成dishDto返回数据
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavor);
        return dishDto;
    }

    @Override
    public void updateDish(DishDto dishDto) {
        // 更新菜品信息
        dishDao.updateDish(dishDto);
        // 删除已存在口味信息
        dishFlavorDao.delByDishId(dishDto.getId());
        // 重新组装并添加口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(dishDto.getCreateTime());
            flavor.setUpdateTime(dishDto.getUpdateTime());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            flavor.setIsDeleted(0);
        }
        dishFlavorDao.bactSave(flavors);
    }

    @Override
    public List<DishDto> getByCategoryId(Long categoryId, Integer status) {

        List<Dish> dishList = dishDao.getByCategoryId(categoryId,status);
        // 转换成dishDto封装其他数据
        List<DishDto> dishDtoList = dishList.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            // 获取口味信息
            List<DishFlavor> dishFlavorList = dishFlavorDao.getByDishId(dish.getId());
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
        return dishDtoList;
    }
}
